﻿function playerForVideo() {
    var video = $("#video");

    // Eventi relativi alla proprietà networkState
    video.bind("loadstart progress suspend emptied stalled", videoNetworkChanged);

    // Eventi relativi alla proprietà readyState
    video.bind("loadedmetadata loadeddata loadeddata waiting playing canplay canplaythrough", videoStateChanged);

    // Eventi relativi alla proprietà pause
    video.bind("play pause", videoStateChanged);

    // Evento di errore
    video.bind("error", videoError);

    video.bind("timeupdate", function (e) {
        var txt = Math.round(e.target.currentTime) + " sec. /" + Math.round(e.target.duration) + " sec.";
        $("#time").text(txt);
    });

    $("#play").click(function () {
        var videoObj = video.get(0);
        if (videoObj.paused)
            videoObj.play();
        else
            videoObj.pause();
    });
}

function videoNetworkChanged(e) {
    // NETWORK_EMPTY
    // NETWORK_IDLE
    // NETWORK_LOADING
    // NETWORK_NO_SOURCE
    if (e.target.networkState == e.target.NETWORK_LOADING)
        $("#time").text("Caricamento...");
}

function videoStateChanged(e) {
    // HAVE_NOTHING
    // HAVE_METADATA
    // HAVE_CURRENT_DATA
    // HAVE_FUTURE_DATA
    // HAVE_ENOUGH_DATA
    if (e.target.readyState == e.target.HAVE_ENOUGH_DATA)
        $("#time").text("Pronto");

    if (e.target.paused)
        $("#play").text("play");
    else
        $("#play").text("pause");
}

function videoError(e) {
    // MEDIA_ERR_ABORTED
    // MEDIA_ERR_NETWORK
    // MEDIA_ERR_DECODE
    // MEDIA_ERR_SRC_NOT_SUPPORTED
    $("#play").text("Error: " + e.target.error.code);
}

$(playerForVideo);